<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-page-header
        content="商家入驻"
        style="padding-bottom: 50px"
        @back="goBack"
      />
      <el-form
        ref="form"
        :model="form"
        label-width="120px"
        label-position="right"
        label-suffix="："
        class="form_style"
        :rules="rules"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="商家名称" prop="title">
              <el-input
                v-model="form.title"
                style="width: 400px"
                placeholder="商家名称"
                clearable
              />
            </el-form-item>
            <el-form-item label="商家详细地址" prop="store_address">
              <el-input
                v-model="form.store_address"
                style="width: 400px"
                placeholder="商家地址"
                clearable
              />
            </el-form-item>
            <el-form-item label="经度" prop="longitude">
              <el-input
                v-model="form.longitude"
                clearable
                style="width: 180px; margin-right: 30px"
                placeholder="经度"
                onkeyup="value=value.replace(/[^1-9]{0,1}(\d*(?:\.\d{0,5})?).*$/g, '$1')"
              />
            </el-form-item>
            <el-form-item label="纬度" prop="latitude">
              <el-input
                v-model="form.latitude"
                style="width: 180px"
                placeholder="纬度"
                clearable
                onkeyup="value=value.replace(/[^1-9]{0,1}(\d*(?:\.\d{0,5})?).*$/g, '$1')"
              />
            </el-form-item>
            <div class="tips">
              （请输入准确的经纬度信息，用于微信小程序地图定位）
            </div>
            <el-form-item label="营业时间" prop="business_hours">
              <el-time-picker
                v-model="form.business_hours"
                style="width: 400px"
                is-range
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                placeholder="选择时间范围"
                value-format="HH:mm"
              />
            </el-form-item>
            <el-form-item label="联系电话" prop="store_phone">
              <el-input
                v-model="form.store_phone"
                style="width: 400px"
                placeholder="联系电话"
                clearable
                onkeyup="value=value.replace(/[^\d]/g,'').replace(/^0{1,}/g,'')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商家公告" prop="announcement">
              <el-input
                v-model="form.announcement"
                type="textarea"
                :rows="3"
                style="width: 400px"
                placeholder="商家公告"
              />
            </el-form-item>
            <el-form-item label="配送信息" prop="delivery_info">
              <el-input
                v-model="form.delivery_info"
                type="textarea"
                style="width: 400px"
                :rows="3"
                placeholder="配送信息"
              />
            </el-form-item>
            <el-form-item label="商家图片" prop="store_imgs">
              <new-upload
                :file_source="'store_imgs'"
                :upload-type="3"
                :format="'.jpg,.jpeg,.png,.JPG,.JPEG,.PNG'"
                :format-txt="'最多上传3张,支持格式：.jpg,.jpeg,.png,.JPG,.JPEG,.PNG'"
                :limit-num="3"
                @reUpload="getUpladImg"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="footer-btn">
        <el-button
          type="primary"
          style="margin-right: 10px"
          @click="add"
        >提交</el-button>
        <el-button @click="goBack">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import NewUpload from "@/components/NewUpload";
import { add_store } from "@/api/store";
export default {
  components: { NewUpload },
  data() {
    return {
      form: {
        store_imgs: [],
      },
      rules: {
        title: [{ required: true, message: "请输入商家名称", trigger: "blur" }],
        store_address: [
          { required: true, message: "请输入商家详细地址", trigger: "blur" },
        ],
        longitude: [{ required: true, message: "请输入经度", trigger: "blur" }],
        latitude: [{ required: true, message: "请输入纬度", trigger: "blur" }],
        business_hours: [
          { required: true, message: "请输入营业时间", trigger: "change" },
        ],
        store_phone: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
        ],
        store_imgs: [
          { required: true, message: "请上传商家图片", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    add() {
      console.log(this.form);
      this.$refs.form.validate((valid) => {
        if (valid) {
          add_store(this.form).then((data)=>{
            console.log('data: ', data);
            if(data.code == 200){
              this.$message.success('添加成功');
              this.goBack()
            }else{
              this.$message.error(data.msg);
            }
          })
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$router.push("/business/list");
    },
    getUpladImg(file_list) {
      console.log("file_list: ", file_list);
      this.form.store_imgs = file_list.join(",");
    },
  },
};
</script>

<style scoped>
.tips {
  margin-left: 120px;
  margin-top: 0;
  margin-bottom: 20px;
  font-size: 14px;
  color: rgb(235, 44, 44);
}
.footer-btn {
  text-align: center;
  margin-top: 50px;
}
</style>
